<?php

function rsSlider_shortcode($atts) {
	
	extract(shortcode_atts(array(
	    'id' => 0,
	    'title' => false,
	    'text' => false,
		'size' => 'full'
	), $atts));
	

	// if Post ID isn't set
	if($atts['id'] == 0) return '<strong>Please set the post id in short code eg. [rsSlider id="27"].</strong>';

	$postid = $atts['id'];
	
		// JAVASCRIPT
	$out = "
	<script type='text/javascript'>
		jQuery(document).ready(function($){
		var mode = null;
		if($(window).width() >=768 && $(window).width()<=991) {
			mode = 'horizontal';
		}else{
			mode = 'vertical';
		}
		var mySwiper = new Swiper('.swiper-container',{
		paginationClickable: true,
		mode: mode,
		loop: true,
		autoplay: 3000,
		slidesPerView: 1
	  })
		$('.arrow-left').on('click', function(e){
			e.preventDefault()
			mySwiper.swipePrev()
		  })
		  $('.arrow-right').on('click', function(e){
			e.preventDefault()
			mySwiper.swipeNext()
		  })
		";

	// End Javascript
	$out .= "
		});
	</script>
	";
	
	$metaSliders 		= get_post_meta($postid, 'rsSlider_repeatable', true); 
	$meta_thumbnail	 	= get_post_meta($postid, 'meta_box_select_rsthumb', true); 
	$meta_display	 	= get_post_meta($postid, 'meta_box_check_text', true); 
	$meta_display_title	= get_post_meta($postid, 'meta_box_check_title', true); 
	
	$att_size = '';
	if(isset($meta_thumbnail))
		$att_size = $meta_thumbnail;
	else
		$att_size = 'full';
		
	
	$image_size = get_thumb_image_width( $att_size );
	if ( !empty( $image_size ) ) {
		$width = $image_size['width'];
		$height = $image_size['height'];
	}

	$hide = false;
	if(isset($meta_display) && $meta_display == 'on')
		$hide = true;

	$hide_title = false;
	if(isset($meta_display_title) && $meta_display_title == 'on')
		$hide_title = true;

		
		
		
	//echo $att_size.' width='.$width.' height='.$height;
	$custom_style = '';
	if($width > 100 && $height > 100)
		$custom_style = 'style="height:'.($height+100).'px ; max-width:'.$width.'px"';
		
		//custom anpham
		
		$out .= '<a class="arrow-left" href="#"></a> 
				<a class="arrow-right" href="#"></a>
			  <div class="swiper-container">
				<div class="swiper-wrapper">';
					if ($metaSliders) {
						foreach($metaSliders as $row) {
							$exp_data = explode("::::", $row);

							$attachment_id = $exp_data[2];
							$this_img = wp_get_attachment_image_src( $attachment_id , $att_size );
							$imgSrc = $this_img[0];
							
							$title = $exp_data[0];
							$description = $exp_data[1];
							$href = $exp_data[3];
							
							#echo nl2br($exp_data[1]);
							$out .= '<div class="swiper-slide">';
							$out .= '<div class="inner">';
								$out .= '<img src='.$imgSrc.' alt="'.$title.'" />';
								
								if($hide == false):
									$out .= '<div class="rsSliderContent">';
										$out .= '<div><a href="'.$href.'" >'.$title.'</a></div>';
										$out .= '<div>'.$description.'</div>';
									$out .= '</div>';
								endif;
								
							$out .= '</div>';
							$out .= '</div>';
									

						}
					}
					$out .= '</div>';
				$out .= '</div>';
		//anpham
	return $out;
}
add_shortcode('rsSlider', 'rsSlider_shortcode');
function get_thumb_image_width( $name ) {
	global $_wp_additional_image_sizes;


	if($name == 'thumbnail'){
		$return_dimensions['width'] = get_option( 'thumbnail_size_w');
		$return_dimensions['height'] = get_option( 'thumbnail_size_h');
		return $return_dimensions;
	}
	elseif($name == 'medium'){
		$return_dimensions['width'] = get_option( 'medium_size_w');
		$return_dimensions['height'] = get_option( 'medium_size_h');
		return $return_dimensions;
	}
	elseif($name == 'large'){
		$return_dimensions['width'] = get_option( 'large_size_w');
		$return_dimensions['height'] = get_option( 'large_size_h');
		return $return_dimensions;
	}
	elseif ( isset( $_wp_additional_image_sizes[$name] ) ){
		return $_wp_additional_image_sizes[$name];
	}

	return false;
}